package com.demo;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Chen Jie
 * 2018/4/12.
 */
public class ForeachList {
    @Test
    public void testForeach() {
        List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API",
                "Date and Time API");

        features.forEach(ForeachList::println);
    }

    private static void println(String s) {
        System.out.println(s);
    }

    @Test
    public void testMapReduce1() {
        List<Integer> list = Arrays.asList(100, 200, 300, 400, 500);
        Integer res1 = 0;
        for (Integer item : list) {
            res1 += item * 2;
        }
        System.out.println(res1);



        Integer res = list.stream()
                .map((num) -> num * 2)
                .reduce((sum, num) -> sum + num)
                .get();
        System.out.println(res);
    }


    @Test
    public void testMapReduce2() {
        List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API",
                "Date and Time API");

        String res = features.stream()
                .map((s) -> s.toUpperCase())
                .collect(Collectors.joining(","));

        System.out.println(res);
    }
}
